package com.dayuanit.dy15.lesson25.demo3;

public class TestFinally {

    public static String a(int num) {
        try {
            System.out.println("num=" + num);
            if (num == 5) {
                throw new IllegalArgumentException("非法数字");
            }
            return "ok";
        } catch (Exception e) {
            System.out.println("catch begin");
            e.printStackTrace();
            System.out.println("catch end");
            return "catch";
        } finally {
            System.out.println("finally");
        }
    }

    public static String b(int num) {
        String msg = " b()";
        try {
            System.out.println("num=" + num);
            if (num == 5) {
                throw new IllegalArgumentException("非法数字");
            }
            return "ok" + msg;//return的顺序：先执行return后面的表达式，将表达式结果先保存，再实行finally代码块，最后再将结果返回，所以，finally代码块不会对return结果产生影响。
        } catch (Exception e) {
            System.out.println("catch begin");
            e.printStackTrace();
            System.out.println("catch end");
            return "catch" + msg;
        } finally {
            msg = " xxx";
            System.out.println("finally " + msg);
        }
    }

    public static int c() {
        int num = 5;
        try {
//            return ++num;//6
            return num++;//5
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally " + num ++);
            System.out.println("finally end " + num);
        }
        return num;
    }

    public static void main(String[] args) {
//        System.out.println(">>>>>" + b(50));
//        System.out.println(">>>>>" + c());

        Integer a = Integer.valueOf(100);
        Integer b = 100;

        //享元设计模式
        Integer d = Integer.valueOf(200);
        Integer c = 200;
        System.out.println(a == b);//true
        System.out.println(c == d);//false
        System.out.println(c.equals(d));//true

        int i = d.intValue();
        int e = d;
    }
}
